//
//  HNHappyTimeFMDB.m
//  XDEducation
//
//  Created by 星道 on 17/8/3.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "HNHappyTimeFMDB.h"
#import <FMDB.h>
@implementation HNHappyTimeFMDB
{
    FMDatabase *_DB;
    NSNumber *_ID;
}

/** 关闭数据库 */
- (void)CloseSql
{
    [_DB close];
}


/**
 实例化，并打开数据库
 */
- (instancetype)initWithID:(NSNumber *)ID
{
    self = [super init];
    if (self)
    {
        _ID = ID;
        // 该帐户的地址
        // 数据库地址
        NSString *DBPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingString:@"/TalkAbout"];
        // 判断文件目录是否存在
        NSFileManager *Manager = [NSFileManager defaultManager];
        // 判断
        BOOL IsHave = [Manager fileExistsAtPath:DBPath];
        // 不存在就创建
        if (IsHave == NO)
        {
            [Manager createDirectoryAtPath:DBPath withIntermediateDirectories:YES attributes:nil error:NULL];
        }
        DBPath = [NSString stringWithFormat:@"%@/TalkAbout_%@.sqlite",DBPath,[AccountTool shareAccountTool].currentAccount.uid];
        // 获取数据库
        _DB = [FMDatabase databaseWithPath:DBPath];
        // 连接数据库
        [_DB open];
    }
    return self;
}

/** 查询对应ID的数据 */
- (NSArray <HNHappyTimeTalkModel *>*)QueryData
{
    NSString *Str = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS T_%@ (id integer PRIMARY KEY AUTOINCREMENT, Uid text NOT NULL, Name text NOT NULL, Content text NOT NULL, Img text NOT NULL, Time text NOT NULL,Status text NOT NULL)",[_ID stringValue]];
    [_DB executeUpdate:Str];

    NSMutableArray *Arr = [NSMutableArray array];
    FMResultSet *rs = [_DB executeQuery:[NSString stringWithFormat:@"select * from T_%@",[_ID stringValue]]];
    while ([rs  next])
    {
        HNHappyTimeTalkModel *Model = [[HNHappyTimeTalkModel alloc] init];
        Model.Uid = @([rs intForColumn:@"Uid"]);
        Model.Name = [rs stringForColumn:@"Name"];
        Model.Time = [rs stringForColumn:@"time"];
        Model.Content = [rs stringForColumn:@"Content"];
        Model.Status = [rs intForColumn:@"Status"];
        Model.Img = [rs stringForColumn:@"Img"];
        [Arr addObject:Model];
    }
    return [Arr copy];
}

/** 播放数据，传入聊天的内容 */
- (void)InsertDataWithStr:(NSString *)Str Complete:(void(^)(BOOL,HNHappyTimeTalkModel *))Complete
{
    HNHappyTimeTalkModel *Model = [[HNHappyTimeTalkModel alloc] init];
    Model.Name = [[AccountTool shareAccountTool].currentAccount.petName isKindOfClass:[NSNull class]] ? [AccountTool shareAccountTool].currentAccount.username:[AccountTool shareAccountTool].currentAccount.petName;
    Model.Img = [AccountTool shareAccountTool].currentAccount.avatar;
    Model.Time = [self getCurrentTimes];
    Model.Content = Str;
    Model.Uid = @([[AccountTool shareAccountTool].currentAccount.uid integerValue]);
    Model.Status = Sending;
    Str = [NSString stringWithFormat:@"INSERT INTO T_%@ (Uid, Name, Time, Img, Content, Status) VALUES (?,?,?, ?, ?, ?)",[_ID stringValue]];;

    if (Complete)
    {
        Complete ([_DB executeUpdate:Str,[Model.Uid stringValue],Model.Name,Model.Time, Model.Img,Model.Content,[@(Model.Status) stringValue]],Model);
    }
}

/** 获取当前时间 */
- (NSString*)getCurrentTimes
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    //现在时间,你可以输出来看下是什么格式
    NSDate *datenow = [NSDate date];
    //----------将nsdate按formatter格式转成nsstring
    NSString *currentTimeString = [formatter stringFromDate:datenow];
    NSLog(@"currentTimeString =  %@",currentTimeString);
    return currentTimeString;
}
@end
